package com.jiepos.mpos.biz.analysis.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.LogStlInsSumDao;
import com.jiepos.mpos.common.entity.App_Log_Stl_Ins_SumEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.StringUtils;


/**
 *
 *
 * <p>标题: —— 机构日交易汇总统计服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.analysis.service
 * @author 余翼膑
 * @version 1.0
 * @since 2018年02月27号
 * @history（历次修订内容、修订人、修订时间等）
 */


@Service
public class LogStlInsSumAnalysisService extends BaseService {
    @Autowired
    private LogStlInsSumDao logStlInsSumDao;

    /**
     * 查询信息列表json分页数据
     * @param pagination
     * @param appLogStlInsSumEntity
     * @return
     */
    public Map getLogStlInsSumAnalysisList(HttpServletRequest request, Pagination pagination, App_Log_Stl_Ins_SumEntity appLogStlInsSumEntity,String start_dt,String end_dt){

        String ins_uuid = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_UUID));
        Calendar calendar = Calendar.getInstance();
        //得到前一天
        calendar.add(Calendar.DATE, -1);
        String  yestedayDate = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());

        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT max(T1.Stl_Dt) as Stl_Dt, ")
          .append(" max(t1.ins_cd) as ins_cd,")
          .append(" max(t1.ins_cn_nm) as ins_cn_nm,")
          .append(" sum(t1.stl_amt) as stl_amt,")
          .append(" sum(t1.cnt) as cnt")
          .append(" FROM "+ DBConstant.Table.T_APP_LOG_STL_INS_SUM+" t1 LEFT JOIN ")
          .append(" "+ DBConstant.Table.T_APP_INS_INF+" t2 on t1.ins_cd = t2.ins_cd")
          .append( " where 1 = 1 and t1.Service_No = "+ SystemConstant.SERVICE_NO_100001+" ");
        List param = new ArrayList();
        //机构号
        if (!StringUtils.isBlank(appLogStlInsSumEntity.getIns_cd())) {
            sb.append( " and t1.ins_cd like ? " );
            param.add("%" + appLogStlInsSumEntity.getIns_cd() + "%");
        }
        //机构名称
        if (!StringUtils.isBlank(appLogStlInsSumEntity.getIns_cn_nm())) {
            sb.append( " and t1.ins_cn_nm like ? " );
            param.add("%" + appLogStlInsSumEntity.getIns_cn_nm() + "%");
        }
        //起止日期
        if (!StringUtils.isBlank(start_dt)) {
            Date dateParam = DateUtil.strToDate(start_dt, DateUtil.FORMAT_DATE_1);
            sb.append("and t1.Stl_Dt >= ? ");
            param.add(DateUtil.dateToStr(dateParam, "yyyyMMdd"));
        }else{
            sb.append("and t1.Stl_Dt >= ? ");
            param.add(yestedayDate);
        }
        if (!StringUtils.isBlank(end_dt)) {
            Date dateParam = DateUtil.strToDate(end_dt, DateUtil.FORMAT_DATE_1);
            sb.append(" and t1.Stl_Dt <= ? ");
            param.add(DateUtil.dateToStr(dateParam, "yyyyMMdd"));
        }else{
            sb.append(" and t1.Stl_Dt <= ? ");
            param.add(yestedayDate);
        }
        sb.append(" and  t2.ins_uuid in ");
        sb.append(" (select tt.ins_uuid from "+ DBConstant.Table.T_APP_INS_INF+" tt ");
        sb.append(" start with tt.ins_uuid='" + ins_uuid + "'");
        sb.append(" connect by prior tt.ins_uuid = tt.sup_ins_uuid)");
       // pagination = getInsAuthSql(request, pagination, "t2", null);
        sb.append("  group by t1.stl_dt ")
                .append(" order by t1.stl_dt desc");
        pagination.queryString = sb.toString();
        return logStlInsSumDao.getPaginationMap(pagination,"t1",param);

    }

}
